/// *******
/// 通用控件
///
/// *******
//export 'password_field.dart';
//export 'sliver_heander.dart';
//export 'extend_tab/extended_tabs.dart';
export 'webview_config.dart';
export 'sliver_persistent_header.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:kunlun_gas/app_files.dart';
import '../../config/config.dart';
import '../../PublicSource/utils/common_utils.dart';
export 'fileimage_ex.dart';

//import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

export 'custom_refresh_widget.dart';
export 'photo_album_widget.dart';
export 'draggable_imagepicker_widget.dart';
export 'tabs.dart';

/// ***********************************
/// 网络加载图片控件
///
/// ***********************************
class CustomImage extends StatelessWidget {
  final dynamic imageUrl;
  final String placeholderUrl;
  final double width;
  final double height;
  final BoxFit fit;
  final Widget placeWidget;

  const CustomImage(
      this.imageUrl, {
        this.width,
        this.height,
        this.fit,
        this.placeholderUrl = '',
        this.placeWidget,
      });

  @override
  Widget build(BuildContext context) {
    if (imageUrl is AssetEntityFile) {
      return CustomExtentedImage(
        imageUrl,
        width: width,
        height: height,
        fit: fit,
      );
    }
    if (imageUrl != null && imageUrl.isNotEmpty) {
      String url = imageUrl.toString().split('|').last;
      return Stack(
        children: [
          Container(
            height: height,
            width: width,
            child: CachedNetworkImage(
              width: width,
              height: height,
              fit: fit,
              imageUrl: NetworkConfig.imageUrl(url),
              placeholder: (context, url) {
                return placeWidget ?? placeholderUrl.length == 0
                    ? Container(
                  decoration: BoxDecoration(
                    color: Colors.grey,
                  ),
                )
                    : Image.asset(
                  placeholderUrl,
                  width: width,
                  height: height,
                );
              },
            ),
          ),
          Positioned(
            right: 0,
            left: 0,
            top: 0,
            bottom: 0,
            child: imageUrl.toString().split('|').first.endsWith('mp4')
                ? Center(
              child: Image.asset(
                'images/ic_play.png',
                width: 40,
                height: 40,
                fit: BoxFit.fitWidth,
              ),
            )
                : Container(),
          ),
        ],
      );
    }

    return placeholderUrl.length != 0
        ? Image.asset(
      placeholderUrl,
      width: width,
      height: height,
    )
        : Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: Colors.grey,
      ),
    );
  }
}

class CustomExtentedImage extends StatelessWidget {
  final AssetEntityFile file;
  final double width;
  final double height;
  final BoxFit fit;

  const CustomExtentedImage(
      this.file, {
        this.width,
        this.height,
        this.fit,
      });

  @override
  Widget build(BuildContext context) {
    if (file.thumbFile != null) {
      return Stack(
        children: [
          Positioned.fill(
            child: ExtendedImage.file(
              file.thumbFile,
              width: width,
              height: height,
              fit: fit,
            ),
          ),
          Positioned(
            right: 0,
            left: 0,
            top: 0,
            bottom: 0,
            child: Center(
              child: Image.asset(
                'images/ic_play.png',
                width: 40,
                height: 40,
                fit: BoxFit.fitWidth,
              ),
            ),
          ),
        ],
      );
    }
    if (file.file != null) {
      return ExtendedImage.file(
        file.file,
        width: width,
        height: height,
        fit: fit,
      );
    }
    return ExtendedImage.network(
      file.url,
      width: width,
      height: height,
      fit: fit,
    );
  }
}

class Badge extends StatelessWidget {
  final Color backgroundColor;
  final Color numberTextColor;
  final int number;

  const Badge(
      {Key key,
      this.backgroundColor = ThemeConfig.mainColor,
      this.numberTextColor = Colors.white,
      this.number})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.only(left: 3, right: 3),
          constraints: BoxConstraints(
            minWidth: 16,
            minHeight: 16,
          ),
          decoration: new BoxDecoration(
            color: backgroundColor,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Text(
            '${number > 99 ? '99+' : number}',
            style: new TextStyle(
              color: numberTextColor,
              fontSize: 10,
              fontWeight: FontWeight.w500,
            ),
          ),
        )
      ],
    );
  }
}

class IconWithBadge extends StatelessWidget {
  final String icon;
  int badgeNumber;

  IconWithBadge(
    this.icon, {
    Key key,
    this.badgeNumber = 0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.centerLeft,
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(top: 2, left: 12, right: 12, bottom: 2),
          child: Image.asset(icon),
        ),
        Positioned(
          top: 0,
          right: 0,
          child:
              badgeNumber <= 0 ? Container() : Badge(number: this.badgeNumber),
        )
      ],
    );
  }
}

class MyLoadingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Stack(
          children: <Widget>[
            SpinKitCircle(color: ThemeConfig.mainColor),
          ],
        ),
      ),
    );
  }
}

class MyEmptyItemWidget extends StatelessWidget {
  final String desc;
  final String image;
  final double padding;

  const MyEmptyItemWidget({
    this.desc: '暂无数据',
    this.image: 'images/bg_empty.png',
    this.padding: 80,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: padding),
      child: Column(
        children: <Widget>[
          Image.asset(image),
          Padding(
            padding: EdgeInsets.only(top: 16),
            child: Text(
              desc,
              style: TextThemeConfig.Font14WithColor66,
            ),
          ),
        ],
      ),
    );
  }
}

AppBar myAppBar(
  BuildContext context, {
  String title = '',
  Widget leading,
  List<Widget> actions,
  bool centerTitle = true,
}) {
  return AppBar(
    backgroundColor: ThemeConfig.mainColor,
    elevation: 0,
    centerTitle: centerTitle,
    title: Text(
      title,
      style: TextThemeConfig.AppBarTitle,
    ),
    leading: leading ??
        GestureDetector(
          onTap: () => Navigator.pop(context),
          behavior: HitTestBehavior.translucent,
          child: Container(
            width: 40,
            height: 40,
            child: Center(
              child: Icon(
                CupertinoIcons.back,
                color: Colors.white,
              ),
            ),
          ),
        ),
    actions: actions,
  );
}

class CustomRoute extends PageRouteBuilder {
  final Widget widget;

  CustomRoute(this.widget)
      : super(
            // 设置过度时间
            transitionDuration: Duration(milliseconds: 250),
            // 构造器
            pageBuilder: (
              // 上下文和动画
              BuildContext context,
              Animation<double> animaton1,
              Animation<double> animaton2,
            ) {
              return widget;
            },
            transitionsBuilder: (
              BuildContext context,
              Animation<double> animaton1,
              Animation<double> animaton2,
              Widget child,
            ) {
              // 需要什么效果把注释打开就行了
              // 渐变效果
              return FadeTransition(
                // 从0开始到1
                opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                  // 传入设置的动画
                  parent: animaton1,
                  // 设置效果，快进漫出   这里有很多内置的效果
                  curve: Curves.fastOutSlowIn,
                )),
                child: child,
              );

              // 缩放动画效果
              // return ScaleTransition(
              //   scale: Tween(begin: 0.0,end: 1.0).animate(CurvedAnimation(
              //     parent: animaton1,
              //     curve: Curves.fastOutSlowIn
              //   )),
              //   child: child,
              // );

              // 旋转加缩放动画效果
              // return RotationTransition(
              //   turns: Tween(begin: 0.0,end: 1.0)
              //   .animate(CurvedAnimation(
              //     parent: animaton1,
              //     curve: Curves.fastOutSlowIn,
              //   )),
              //   child: ScaleTransition(
              //     scale: Tween(begin: 0.0,end: 1.0)
              //     .animate(CurvedAnimation(
              //       parent: animaton1,
              //       curve: Curves.fastOutSlowIn
              //     )),
              //     child: child,
              //   ),
              // );

              // 左右滑动动画效果
              // return SlideTransition(
              //   position: Tween<Offset>(
              //     // 设置滑动的 X , Y 轴
              //     begin: Offset(-1.0, 0.0),
              //     end: Offset(0.0,0.0)
              //   ).animate(CurvedAnimation(
              //     parent: animaton1,
              //     curve: Curves.fastOutSlowIn
              //   )),
              //   child: child,
              // );
            });
}

class MyDismissWidget extends StatelessWidget {
  final Key key;
  final Widget child;
  final String confirmDismissMessage;
  final Function onDelete;

  const MyDismissWidget(
      {this.key,
      this.child,
      this.onDelete,
      this.confirmDismissMessage = '确定删除该条信息？'});

  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: key,
      child: child,
      onDismissed:
          onDelete == null ? null : (DismissDirection direction) => onDelete(),
      confirmDismiss: (DismissDirection direction) async {
        int res =
            await CommonUtils.showOKCanceltView(context, confirmDismissMessage);
        if (res == 1)
          return true;
        else
          return false;
      },
      direction: DismissDirection.endToStart,
      background: Container(
        // 这里使用 ListTile 因为可以快速设置左右两端的Icon
        child: Row(
          children: <Widget>[
            Expanded(child: Container()),
            Container(
              width: 50,
              height: double.infinity,
              child: Center(
                child: Icon(
                  Icons.delete,
                  color: ThemeConfig.mainColor,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class MySearchWidget extends StatelessWidget {
  final int type;
  final String heroTag;
  final double padding;
  final String searchText;
  final String communityID;
  final String communityName;
  final String planID;
  final String workID;
  final int status;

  const MySearchWidget({
    this.type,
    this.heroTag = 'search',
    this.padding = 15,
    this.searchText = '搜索',
    this.communityID,
    this.communityName,
    this.planID,
    this.workID,
    this.status,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Hero(
        tag: heroTag,
        child: Material(
          color: ThemeConfig.mainColor,
          child: Container(
            margin: EdgeInsets.all(padding),
            height: 40,
            padding: EdgeInsets.only(left: 14),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(
                Radius.circular(20),
              ),
            ),
            child: Row(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(bottom: 1),
                  child: Image.asset('images/icon_search.png'),
                ),
                Padding(padding: EdgeInsets.only(left: 8)),
                Expanded(
                  child: TextField(
                    style: TextStyle(fontSize: 14),
                    enabled: false,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: searchText,
                      hintStyle: TextStyle(
                          color: ThemeConfig.black153Color, fontSize: 14),
                      contentPadding: EdgeInsets.only(bottom: 9),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      onTap: () => CommonUtils.showSearchView(
        context,
        type: type,
        heroTag: heroTag,
        communityID: communityID,
        communityName: communityName,
        planID: planID,
        workID: workID,
        status: status,
      ),
    );
  }
}

class MyProgressWidget extends StatelessWidget {
  final double height;
  final double value;
  final Color backColor;
  final Color color;

  const MyProgressWidget({
    this.height = 10,
    this.backColor = const Color(0xffdbdbdb),
    this.color = ThemeConfig.mainColor,
    @required this.value,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      child: Row(
        children: [
          Expanded(
            child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(height / 2)),
              child: Container(
                color: backColor,
                child: Row(
                  children: [
                    Expanded(
                      flex: (100 * value).toInt(),
                      child: Container(
                        decoration: BoxDecoration(
                          color: color,
                          borderRadius:
                              BorderRadius.all(Radius.circular(height / 2)),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Container(),
                      flex: ((1 - value) * 100).toInt(),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class MyInkWellWidget extends StatelessWidget {
  final Widget child;
  final GestureTapCallback onTap;

  const MyInkWellWidget({this.child, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: child,
    );
  }
}
